package com.github.andy0106.kersstcli.client;

import com.github.andy0106.kersstjtl.api.Package;
import com.github.andy0106.kersstjtl.api.PackagesCache;
import com.github.andy0106.kersstjtl.files.Dir;
import com.google.gson.Gson;

import java.nio.file.Paths;
import java.util.Collection;

public class Local {
    private static Dir MCDIR = new Dir(".minecraft/versions/", ".");
    private static PackagesCache packages_cache = new PackagesCache();
    public static final Gson gson = new Gson();

    public static Collection<String> getAllPackagesName() {
        return packages_cache.packages.keySet();
    }

    public static Package getPackageByName(String name) {
        return packages_cache.packages.get(name);
    }

    public static String getMCDIR() {
        return MCDIR.toString();
    }

    public static void setMCDIR(String dir) {
        MCDIR.setPaths(Paths.get(dir, "/versions/"));
        MCDIR.walk();
        MCDIR.getChildren_dir().forEach(_dir -> {
            _dir.getChildren_file().forEach(file -> {
                if (file.getName().endsWith(".pack")) {
                    String[] s = file.getName().split("-");
                    Package p = gson.fromJson(file.readString_UTF_8(), Package.class);
                    packages_cache.packages.put(s[0], p);
                }
            });
        });
    }
}
